//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetSocketAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.CoapClient.Builder;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.server.resources.Resource;
import server.MyResource;

public class Simulator extends JFrame {
    private JTextField tfCigServerAddress = new JTextField();
    private JTextField tfCigServerPort = new JTextField();
    private JTextField tfEp = new JTextField();
    private JTextPane console = new MyTextPane();
    private JButton btnRegister = new JButton("注册设备");
    private JTextPane hexMsgArea = new MyTextPane();
    private JTextPane jsonMsgArea = new MyTextPane();
    private JButton btnSendMessage = new JButton("发消息");
    private JButton btnClearLog = new JButton("清空日志");
    private JTabbedPane tabbedPane = new JTabbedPane();
    private CoapServer server;
    private static int COAP_PORT = 5683;
    private MyResource resource;
    private static Simulator instance = new Simulator();

    private Simulator() {
        super("CoAP UE Simulator");
    }

    public static Simulator getInstance() {
        return null == instance ? new Simulator() : instance;
    }

    public void printConsole(String message, Object... params) {
        try {
            Style style = this.console.addStyle("free", (Style)null);
            StyleConstants.setFontSize(style, 14);
            String str = String.format(message, params) + "\r\n";
            this.console.getDocument().insertString(this.console.getDocument().getLength(), str, style);
            str = "===========================================================\r\n";
            this.console.getDocument().insertString(this.console.getDocument().getLength(), str, style);
        } catch (BadLocationException var5) {
            var5.printStackTrace();
        }

    }

    public void addComponents(Container pane) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pane.setSize(520, 650);
        pane.setPreferredSize(new Dimension(520, 650));
        this.setLocation(screenSize.width / 2 - pane.getWidth() / 2, screenSize.height / 2 - pane.getHeight() / 2);
        BorderLayout mainLayout = new BorderLayout();
        pane.setLayout(mainLayout);
        Dimension labelFieldSize = new Dimension(150, 24);
        JPanel top = new JPanel();
        top.setPreferredSize(new Dimension(500, 65));
        JLabel labelCigServer = new JLabel("CIG server:");
        labelCigServer.setPreferredSize(labelFieldSize);
        labelCigServer.setHorizontalAlignment(0);
        this.tfCigServerAddress.setPreferredSize(new Dimension(200, 24));
        this.tfCigServerPort.setPreferredSize(new Dimension(90, 24));
        top.add(labelCigServer);
        top.add(this.tfCigServerAddress);
        top.add(new JLabel(":"));
        top.add(this.tfCigServerPort);
        JLabel labelEp = new JLabel("ep=");
        labelEp.setPreferredSize(labelFieldSize);
        labelEp.setHorizontalAlignment(0);
        this.tfEp.setPreferredSize(new Dimension(200, 24));
        top.add(labelEp);
        top.add(this.tfEp);
        this.btnRegister.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String cigAddress = Simulator.this.tfCigServerAddress.getText();
                int cigPort = 0;
                String ep = Simulator.this.tfEp.getText();

                try {
                    cigPort = Integer.parseInt(Simulator.this.tfCigServerPort.getText());
                } catch (NumberFormatException var11) {
                    Simulator.getInstance().printConsole("cig port is incorrect.");
                }

                Simulator.this.server.destroy();
                Simulator.this.startServer();
                int i = Simulator.this.tabbedPane.getSelectedIndex();
                String payload;
                switch(i) {
                    case 0:
                        payload = Simulator.this.hexMsgArea.getText();
                        break;
                    case 1:
                        payload = Simulator.this.jsonMsgArea.getText();
                        break;
                    default:
                        payload = null;
                }

                Simulator.this.resource.setData(i, payload);
                Builder builder = new Builder(cigAddress, cigPort);
                CoapClient coapClient = builder.query(new String[]{"ep=" + ep}).path(new String[]{"t", "r"}).create();
                coapClient.setEndpoint((Endpoint)Simulator.this.server.getEndpoints().get(0));
                byte[] data = null;
                CoapResponse response = coapClient.post((byte[])data, 42);
                Simulator.getInstance().printConsole("响应码 %s.", response.getCode());
            }
        });
        this.btnRegister.setPreferredSize(new Dimension(95, 24));
        top.add(this.btnRegister);
        pane.add(top, "North");
        JPanel center = new JPanel();
        center.setPreferredSize(new Dimension(500, 300));
        this.console.setPreferredSize(new Dimension(500, 270));
        this.console.setSize(500, 270);
        JScrollPane scrollPane = new JScrollPane(this.console, 22, 31);
        scrollPane.setPreferredSize(new Dimension(500, 270));
        center.add(scrollPane);
        pane.add(center, "Center");
        JPanel bottom = new JPanel();
        bottom.setPreferredSize(new Dimension(500, 300));
        this.tabbedPane.setPreferredSize(new Dimension(500, 230));
        JPanel hexMsgPanel = new MyOutMessagePane("Hex消息:", this.hexMsgArea, 490, 200);
        this.tabbedPane.add("十六进制码流", hexMsgPanel);
        JPanel jsonMsgPanel = new MyOutMessagePane("JSON消息:", this.jsonMsgArea, 490, 200);
        this.tabbedPane.add("JSON", jsonMsgPanel);
        bottom.add(this.tabbedPane);
        this.btnSendMessage.setPreferredSize(new Dimension(95, 24));
        this.btnSendMessage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (Simulator.this.resource == null) {
                    Simulator.getInstance().printConsole("请先注册！");
                }

                int i = Simulator.this.tabbedPane.getSelectedIndex();
                String payload;
                switch(i) {
                    case 0:
                        payload = Simulator.this.hexMsgArea.getText();
                        break;
                    case 1:
                        payload = Simulator.this.jsonMsgArea.getText();
                        break;
                    default:
                        payload = null;
                }

                Simulator.this.resource.stateChanged(i, payload);
            }
        });
        this.btnClearLog.setPreferredSize(new Dimension(95, 24));
        this.btnClearLog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Simulator.this.console.setText("");
            }
        });
        bottom.add(this.btnSendMessage);
        bottom.add(this.btnClearLog);
        pane.add(bottom, "South");
        pane.setVisible(true);
    }

    public void startServer() {
        try {
            this.server = new CoapServer(COAP_PORT);
            CoapResource rootResource = new CoapResource("t");
            this.resource = new MyResource("d");
            rootResource.add(this.resource);
            this.server.add(rootResource);
            this.server.start();
            InetSocketAddress inetSocketAddress = this.resource.getEndpoints().get(0).getAddress();
            getInstance().printConsole("UE CoAP server start. %s:%s", inetSocketAddress.getAddress().getHostAddress(), inetSocketAddress.getPort());
        } catch (Exception e) {
            getInstance().printConsole(e.getMessage());
        }

    }

    public static void init() {
        getInstance().setDefaultCloseOperation(3);
        getInstance().addComponents(getInstance().getContentPane());
        getInstance().pack();
        getInstance().setVisible(true);
        String input = JOptionPane.showInputDialog(instance, "UE服务器端口:");

        try {
            COAP_PORT = Integer.parseInt(input);
            getInstance().startServer();
        } catch (NumberFormatException var2) {
            JOptionPane.showMessageDialog(instance, "端口错误。");
        }

    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException var2) {
            var2.printStackTrace();
        } catch (IllegalAccessException var3) {
            var3.printStackTrace();
        } catch (InstantiationException var4) {
            var4.printStackTrace();
        } catch (ClassNotFoundException var5) {
            var5.printStackTrace();
        }

        UIManager.put("swing.boldMetal", Boolean.FALSE);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Simulator.init();
            }
        });
    }
}
